package com.example.lotterysystem.service.impl;

import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.dao.dataobject.PrizeDO;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PrizeDTO;
import com.example.lotterysystem.service.dto.PageListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/5/21  20:33
 */
@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        // 1.奖品总数
        Integer count = prizeMapper.count();
        // 2.当前页奖品信息
        List<PrizeDTO> prizeDTOS = new ArrayList<>();
        List<PrizeDO> prizeDOS = prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
        for (PrizeDO p : prizeDOS) {
            PrizeDTO prizeDTO=new PrizeDTO();
            prizeDTO.setPrizeId(p.getId());
            prizeDTO.setDescription(p.getDescription());
            prizeDTO.setName(p.getName());
            prizeDTO.setPrice(p.getPrice());
            prizeDTO.setImageUrl(p.getImageUrl());

            prizeDTOS.add(prizeDTO);
        }

        return new PageListDTO<PrizeDTO>(count,prizeDTOS);
    }

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile file) {
        // 1. 上传图片
        String url=pictureService.savePicture(file);

        // 2. 保存信息到数据库
        PrizeDO prizeDO=new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(url);
        prizeDO.setPrice(param.getPrice());

        prizeMapper.insert(prizeDO);

        return prizeDO.getId();
    }
}
